<template>
  <a-modal
    :title="isShow ? '修改角色':'新建角色'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="角色名称">
          <a-input v-decorator="['name', {rules: [{required: true, message: '必须选择一个角色名称'}]}]" />
        </a-form-item>
        <a-form-item label="学校权限">
          <a-select v-decorator="['role_type', {rules: [{required: true, message: '必须选择一个学校权限'}]}]" >
            <a-select-option :value="1">监督员权限</a-select-option>
            <a-select-option :value="2">管理员权限</a-select-option>
          </a-select>
        </a-form-item>
        
      </a-form>
      <!-- 树形控件 -->
       <a-tree
        v-model="checkedKeys"
        checkable
        checkStrictly
        :expanded-keys="expandedKeys"
        :auto-expand-parent="autoExpandParent"
        :selected-keys="selectedKeys"
        :tree-data="treeData"
        @expand="onExpand"
        @select="onSelect"
        @check="onCheck"
      />
    </a-spin>
  </a-modal>
</template>

<script>

import pick from 'lodash.pick'
import treeData from '../../../../assets/jurisdiction.json'
// 表单字段
const fields = ['name', 'id', 'role_type']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      isShow: false,
      // 树形控件
      FatChecked: [],
      expandedKeys: ['角色列表', 'roleList'],
      autoExpandParent: true,
      checkedKeys: [],
      selectedKeys: [],
      // 记录按钮的数组
      btnChecked: [],
      // 默认选中
      // defaultSelectedKeys: this.model.owned_role,
      treeData,
      // 树形控件
      allChecked: [],
      form: this.$form.createForm(this)
    }
  },
  watch: {
    checkedKeys(val) {
      console.log('val', val)
      console.log('--------this.allChecked-------------', this.allChecked)
      const btnItems = this.allChecked.filter(item => {
        return item.indexOf(2) !== -1
      })
      const pageItems = this.allChecked.filter(item => {
        return item.indexOf(1) !== -1
      })
      this.$emit('btnChecked', btnItems)
      this.$emit('pageChecked', pageItems)
      
      this.$emit('getAllList', this.allChecked)
      if(val.checked) {
        this.$emit('getCheckList', this.allChecked)
      }else{
        this.$emit('getCheckList', val)
      }

    },
    // 修改
    model(val) {
      console.log(val)
      if(val && val.id > 0){
        this.checkedKeys = val.roles
      }else{
        this.checkedKeys = []
      }
    }
  },
  methods: {
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys);
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onCheck(checkedKeys, info) {
      console.log('onCheck', checkedKeys, info);
      this.checkedKeys = checkedKeys
      this.allChecked = checkedKeys.checked
    },
    onSelect(selectedKeys, info) {
      console.log('onSelect', info);
      this.selectedKeys = selectedKeys;
    },
  },
  created () {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if(this.model && this.model.id > 0) {
        this.isShow = true
      } else {
        this.isShow = false
      }
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
